//package com.jumi.microservice.mq;
//
//
//import cn.hutool.core.util.NumberUtil;
//import com.alibaba.fastjson.JSONObject;
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.jumi.microservice.entity.ErrOrderAfterMsg;
//import com.jumi.microservice.entity.JmUserFenxiao;
//import com.jumi.microservice.enums.CommonEnum;
//import com.jumi.microservice.enums.FenXiaoStatus;
//import com.jumi.microservice.service.IErrOrderAfterMsgService;
//import com.jumi.microservice.service.IJmUserFenxiaoService;
//import com.jumi.microservice.util.WrapperUtil;
//import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
//import org.apache.rocketmq.spring.core.RocketMQListener;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Component;
//
//import java.math.BigDecimal;
//import java.time.LocalDateTime;
//import java.util.List;
//import java.util.Map;
//
///**
// * 订单售后消息队列
// */
//
//@Component
//@RocketMQMessageListener(topic = "orderAfterSale",consumerGroup = "member-orderAfter")
//public class OrderAfterSaleMq implements RocketMQListener<String>{
//
//    @Autowired
//    IJmUserFenxiaoService fenxiaoService;
//    @Autowired
//    IErrOrderAfterMsgService errOrderAfterMsgService;
//
//
//    Logger logger= LoggerFactory.getLogger(OrderAfterSaleMq.class);
//
//    @Override
//    public void onMessage(String message) {
//        try {
//            //{buyNum=6, orderId=102, afterNum=7, skuId=11}
//            Map<String,Integer> map = JSONObject.parseObject(message, Map.class);
//            Integer buyNum = map.get("buyNum");
//            Integer orderId = map.get("orderId");
//            Integer afterNum = map.get("afterNum");
//            Integer skuId = map.get("skuId");
//
//            LocalDateTime now = LocalDateTime.now();
//
//            JmUserFenxiao fenxiao=new JmUserFenxiao();
//            fenxiao.setOrderId(orderId);
//            fenxiao.setGid(skuId);
//            fenxiao.setIsDel(CommonEnum.否);
//            QueryWrapper wrapper = WrapperUtil.getWrapper(null, fenxiao);
//            wrapper.ne("status", FenXiaoStatus.失效);
//
//            List<JmUserFenxiao> list = fenxiaoService.list(wrapper);
//
//            for (JmUserFenxiao jmUserFenxiao : list) {
//                if(afterNum>=buyNum){
//                    jmUserFenxiao.setStatus(FenXiaoStatus.失效);
//                }else{
//                    //售后数量小于购买数量时，需要重新计算佣金;
//                    BigDecimal yongjin = jmUserFenxiao.getYongjin();
//                    double v = yongjin.doubleValue();
//                    //计算出单个商品的佣金
//                    double single = NumberUtil.div(v, buyNum.doubleValue());
//                    //计算出剩余没有售后的商品数量
//                    buyNum-=afterNum;
//                    double mul = NumberUtil.mul(single, buyNum.doubleValue());
//                    yongjin=new BigDecimal(mul);
//                    jmUserFenxiao.setYongjin(yongjin);
//                }
//                jmUserFenxiao.setUpdateTime(now);
//            }
//            if(list.size()>0){
//                fenxiaoService.updateBatchById(list);
//            }
//        }catch (Exception e){
//            //如果处理出错，将消息存储到数据库再处理.
//            ErrOrderAfterMsg errOrderAfterMsg=new ErrOrderAfterMsg();
//            errOrderAfterMsg.setCreateTime(LocalDateTime.now());
//            errOrderAfterMsg.setErrContent(e.getMessage());
//            errOrderAfterMsg.setErrMsg(message);
//            errOrderAfterMsg.setIsDel(CommonEnum.否);
//            errOrderAfterMsgService.save(errOrderAfterMsg);
//            logger.error("订单售后消息处理错误--->:"+e);
//        }
//    }
//}
//
